/*
* Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
*/
package com.hyyt.shopjsp.common.controller;

import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.basic.service.IBasicActorPurviewService;
import com.hyyt.shopjsp.util.redis.service.IRedisBasicService;
import com.hyyt.shopjsp.util.common.StringStaticKey;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.redis.util.RedisDataReadUtil;
import com.hyyt.shopjsp.util.validate.TimeStampPropertyEditor;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;

/**
 * 基本控制层操作类
 *
 * @author ankang
 * @date 2016/7/18 14:47
 */
@Controller
public class BaseController {
    protected static final Logger logger = Logger.getLogger(BaseController.class);
    /**
     * Redis 基础Service接口类
     */
    @Resource
    protected IRedisBasicService redisBasicService;
    /**
     * HttpSession
     */
    @Resource
    protected HttpSession session;

    /**
     * 项目路径
     */
    private static String basePath;


    /**
     * 重定向到其它资源
     *
     * @param url action路径
     * @return 返回重定向路径
     */
    public String redirect(String url) {
        return "redirect:" + url;
    }


    /**
     * 转发到其它资源
     *
     * @param url action路径
     * @return 返回重定向路径
     */
    public String forward(String url) {
        return "forward:" + url;
    }

    /**
     * 获取项目路径
     *
     * @return
     */
    public String getBasePath() {
        if (!Utils.stringIsNotEmpty(basePath)) {
            basePath = RedisDataReadUtil.getSysConfig(StringStaticKey.BASE_PATH_BACK);
        }
        return basePath;
    }

    /**
     * 获取用户session
     *
     * @return
     */
    public BasicUsers getSessionUsers() {
        BasicUsers users = (BasicUsers) session.getAttribute(StringStaticKey.SESSION_USERS);
        return users;
    }

    /**
     * 获取登录帐号用户名
     *
     * @return
     */
    public String getSessionUsersName() {
        BasicUsers users = getSessionUsers();
        return users.getUserName();
    }


    /**
     * 自定义日期TimeStamp类型转换
     *
     * @param request
     * @param binder
     * @throws Exception
     */
    @InitBinder
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
        //yyy/mmdd yyy-mm-dd yy-mm-dd HH:mm:ss
        binder.registerCustomEditor(Timestamp.class, new TimeStampPropertyEditor());
    }

}
